<?php
namespace Home\Controller;

/*
 * 文章管理
 */
class ArticleController extends BaseController {



    /*
     * 构造
     */
    public function __construct()
    {
        parent::__construct();
        $this -> assign('cate' , 'user');
    }



    /*
     *  栏目列表
     */
    public function ArticleColumn(){
        //判断用户级别显示
        $this->userauth();
        //
        $id = I('get.column');
        if($id)
            $where = array('status'=>1 , 'upid'=>$id);
        else
            $where = array('status'=>1);
        $list = M('articlecolumn') -> where($where) -> order('`order` asc,id asc') -> select();
        if($id)  $this->fsucces($list);
        $list = flytreeArray($list);
        $this -> assign('list' , $list);
        $this -> display();
    }

    /*
     *  栏目tree内部循环调用
     */
    public function ColumnTree($lists=null)
    {
        if(!$lists)  return false;
        $this -> assign('lists',$lists);
        $this -> display('ColumnTree');
    }

    /*
     *  栏目添加
     */
    public function ArticleColumnAdd(){
        //获取栏目
        if($_POST){
            //记录行为
            action_log('PC', 'Article_ArticleColumnAdd', UID, UID);
            //
            $save = array(
                'name' => I('post.name'),
                'upid' => I('post.upid'),
                'order' => I('post.order'),
            );
            $id = I('post.id');
            $infoTmp = M('articlecolumn') -> where($save) -> find();
            if($infoTmp && !$id){
                $this->ferror('已存在');
            }
            //save
            $save['ttime'] = time();
            $save['uid'] = $this->userinfo['userid'];
            $save['status'] = 1;
            if ($id) {
                M('articlecolumn')->where(array('id' => $id))->save($save);
                $this -> fsucces('编辑成功');
            } else {
                M('articlecolumn') -> add($save);
                $this -> fsucces('添加成功');
            }
        }
        if(I('get.id'))  $this -> assign('upid' , I('get.id'));
        $this -> display();
    }

    /*
     *  栏目编辑
     */
    public function ArticleColumnEdit(){
        //判断用户级别显示
        $this->userauth();
        //
        //获取栏目
        $this -> assign('menu',getArticleColumn());
        $info = M('articlecolumn') -> where(array('id'=>I('get.id'))) -> find();
        $this -> assign('info' , $info);
        $this -> assign('upid' , $info['upid']);
        $this -> display('ArticleColumnAdd');
    }

    /*
     *  栏目删除
     */
    public function ArticleColumnDel(){
        //记录行为
        action_log('PC', 'Article_ArticleColumnDel', UID, UID);
        //
        //判断用户级别显示
        $this->userauth('json');
        //
        M('articlecolumn') -> where(array('id'=>I("get.id"))) -> save(array('status'=>-1));
        $this -> fsucces('操作成功');
    }



    /*
     *  文章显示
     */
    public function ArticleShow(){
        //判断用户级别显示
        $this->userauth();
        //
        $id = I('get.id');
        $content = M('article') -> where(array('id'=>$id)) -> find();
        $content['content'] = htmlspecialchars_decode($content['content']);
        $this -> assign('content' , $content);
        $this -> display();
    }

    /*
     *  文章列表
     */
    public function ArticleList(){
        //判断用户级别显示
        $this->userauth();
        //
        $id = I('get.column');
        if($id)
            $where = array('status'=>1 , 'column'=>$id);
        else
            $where = array('status'=>1);
        //获取栏目
        $this -> assign('menu',getArticleColumn());
        //
        $list = $this -> flylists("article" , $where ,"*","","id asc");
        foreach($list as $v){
            $v['columnname'] = M('articlecolumn') -> where(array('id'=>$v['column'])) -> getField('name');
            $result[] = $v;
        }
        if($id)  $this->fsucces($result);
        $this -> assign('list' , $result);
        $this -> display();
    }

    /*
     *  文章添加
     */
    public function ArticleWrite(){
        //判断用户级别显示
        $this->userauth();
        //获取栏目
        $this -> assign('menu',getArticleColumn());
        if($_POST){
            //记录行为
            action_log('PC', 'Article_ArticleWrite', UID, UID);
            //
            $save = array(
                'title' => I('post.title'),
                'content' => I('post.content'),
                'description' => I('post.description'),
                'column' => I('post.column')
            );
            $id = I('post.id');
            $infoTmp = M('article') -> where($save) -> find();
            if($infoTmp && !$id){
                $this->ferror('已存在');
            }
            //save
            $save['ttime'] = time();
            $save['uid'] = $this->userinfo['userid'];
            $save['status'] = 1;
            if ($id) {
                M('article') -> where(array('id' => $id)) -> save($save);
                $this -> fsucces('编辑成功');
            } else {
                M('article') -> add($save);
                $this -> fsucces('添加成功');
            }
        }
        $this -> display();
    }

    /*
     *  文章编辑
     */
    public function ArticleEdit(){
        //判断用户级别显示
        $this->userauth();
        //
        //获取栏目
        $this -> assign('menu',getArticleColumn());
        $info = M('article') -> where(array('id'=>I('get.id'))) -> find();
        $this -> assign('info' , $info);
        $this -> display('ArticleWrite');
    }

    /*
     *  文章删除
     */
    public function ArticleDelete(){
        //记录行为
        action_log('PC', 'Article_ArticleDelete', UID, UID);
        //
        //判断用户级别显示
        $this->userauth('json');
        //
        M('article') -> where(array('id'=>I("get.id"))) -> save(array('status'=>-1));
        $this -> fsucces('操作成功');
    }



    /*
     *
     */


}
